// 集中完成路由配置，并抛出
import { Goods, Key, Basketball, Present } from "@element-plus/icons-vue";
// 引入、配置、导出
import { createRouter, createWebHashHistory } from "vue-router";
import Product from "../pages/product/index.vue";
import Account from "../pages/account/index.vue";
import Live from "../pages/live/index.vue";
import Category from "../pages/category/index.vue";
import TestPinia from "../pages/test-pinia/index.vue";
import Login from "../pages/login/index.vue";
import MainLayout from "../layout/index.vue";
import { useAccount } from "../store/user";
import NotFound from "../pages/lost/index.vue";
import DashBoard from "../pages/dashboard/index.vue";

// 记录路径---页面的关系的数据包
export const routes = [
  {
    path: "/",
    component: MainLayout,
    meta: { title: "主面板", icon: Present },
    redirect: "/dashboard",
    //子路由
    children: [
      {
        path: "/prod",
        component: Product,
        meta: { title: "商品管理", icon: Goods }, //路由元信息
      },
      {
        path: "/account",
        component: Account,
        meta: { title: "账户管理", icon: Key },
      },
      {
        path: "/live",
        component: Live,
        meta: { title: "生活馆", icon: Basketball },
      },
      {
        path: "/cate",
        component: Category,
        meta: { title: "分类管理", icon: Present },
      },
      {
        path: "/test/pinia",
        component: TestPinia,
        meta: { title: "测试pinia", icon: Present, hidden: true },
      },
      {
        path: "/dashboard",
        component: DashBoard,
        meta: { title: "数据可视化", icon: Present },
      },
    ],
  },
  {
    path: "/login",
    component: Login,
    meta: { title: "登录", icon: Present },
  },
  {
    path: "/:pathMatch(.*)*",
    component: NotFound,
    meta: { title: "404", icon: Present },
  },
];

export const abc = "Hello ABC"; //测试

export const testFn = () => {
  //测试
  console.log("测试模块化语法");
};

const router = createRouter({
  history: createWebHashHistory(), //配置路由模式，哈希模式
  routes: routes, //路由对应关系
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  console.log("去哪里", to);
  console.log("来自哪里", from);
  let whiteList = ["/login", "/404"]; //路由白名单
  if (whiteList.includes(to.path)) {
    next(); //直接放行
    return;
  }
  // 判断用户登录状态
  const account = useAccount();
  if (account.userInfo) {
    next();
  } else {
    next("/login"); //指定访问一个新路由
  }
});

export default router; //抛出路由对象
